package com.soft.dao;

import com.soft.pojo.Products;
import com.soft.utils.DBUtil;

import java.util.List;
import java.util.UUID;

public class ProductsDAO {
    public List<Products> selectAll(int currentPage, int currentCount, String category) {
        if (category.equals("全部商品")) {
            return DBUtil.query("select * from products limit ?,?", Products.class, currentPage, currentCount);
        } else {
            return DBUtil.query("select * from products where category=? limit ?,?", Products.class, category, currentPage, currentCount);
        }
    }

    public List<Products> selectProductCount(String category) {
        if (category.equals("全部商品")) {
            return DBUtil.query("select count(*) from products", Products.class);
        } else {
            return DBUtil.query("select count(*) from products where category = ?", Products.class, category);
        }
    }

    public List<Products> selectAll() {
        return DBUtil.query("select * from products", Products.class);
    }

    public void insertProducts(Products products) {
        String uuid = UUID.randomUUID().toString();
        DBUtil.excute("insert into products(id,name,price,category,pnum,description) values(?,?,?,?,?,?)", uuid , products.getName(), products.getPrice(), products.getCategory(), products.getPnum(), products.getDescription());
    }

    public void delProductsById(String id) {
        DBUtil.excute("delete from products where id = ?", id);
    }

    public List<Products> selectProductByManyCondition(String id, String name, String category, double minprice, double maxprice) {
        if (!id.equals("") && id != null) {
            return DBUtil.query("select * from products where id = ?", Products.class, id);
        }
        if (!category.equals("") && category != null) {
            return DBUtil.query("select * from products where category = ?", Products.class, category);
        }
        if (!name.equals("") && name != null) {
            return DBUtil.query("select * from products where name = ?", Products.class, name);
        }
        if (minprice > 0) {
            return DBUtil.query("select * from products where price > ?", Products.class, minprice);
        }
        if (maxprice > 0) {
            return DBUtil.query("select * from products where price < ?", Products.class, maxprice);
        } else {
            return DBUtil.query("select * from products", Products.class);
        }
    }

    public Products findProductsById(String id) {
        List<Products> products = DBUtil.query("select * from products where id=?", Products.class, id);
        if (products.size() > 0) {
            return products.get(0);
        }
        return null;
    }

    public void updateProducts(Products products) {
        DBUtil.excute("update products set name=?,price=?,category=?,pnum=?,description=? where id=?", products.getName(), products.getPrice(), products.getCategory(), products.getPnum(), products.getDescription(), products.getId());
    }
}
